// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

#include <fmt/format.h>
#include <google/protobuf/message.h>
#include <grpcpp/grpcpp.h>

#include "coin2/base/proto_util.h"
#include "coin2/exchange/base/order/order_context_manager.h"

namespace coin2::exchange::base::api_base::grpc {

struct GrpcContext {
  int query_type;
  ::grpc::ClientContext context;                          // context
  std::shared_ptr<::google::protobuf::Message> request;   // request
  ::grpc::Status status;                                  // status
  std::shared_ptr<::google::protobuf::Message> response;  // response
  const coin2::exchange::base::order::OrderContext* oc;   // order context

  GrpcContext() = default;
  GrpcContext(const GrpcContext&) = delete;
  GrpcContext& operator=(const GrpcContext&) = delete;

  std::string ToJsonString() {
    std::string request_json = ProtoToJsonString(*request.get());
    std::string response_json = ProtoToJsonString(*response.get());

    return fmt::format(
        R"({{"query_type":{}, "request":{}, "error_code":{}, "error_message":"{}", "response":{}}})",
        query_type,
        request_json,
        status.error_code(),
        status.error_message(),
        response_json);
  }
};

}  // namespace coin2::exchange::base::api_base::grpc
